package com.aix.demo.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aix.android.framework.activity.BaseActivity;
import com.aix.demo.R;

public class ViewActivity extends BaseActivity {
	
	private static final String TAG = "ViewActivity";
	
	private LinearLayout layout;
	
	private TextView mTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_view);
		
		layout = (LinearLayout) findViewById(R.id.layout);
		mTextView = (TextView) findViewById(R.id.textview);
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		
		for (int i = 0; i < 35; ++i) {
			TextView textView = new TextView(this);
			textView.setText("TextView");
			
			layout.addView(textView, params);
		}
		
		Button btn = new Button(this);
		btn.setText("click me");
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int[] location = new int[2];
				layout.getLocationInWindow(location);
				
				StringBuilder builder = new StringBuilder();
				builder.append("LinearLayout LocInWindow : " + location[0] + " " + location[1]);
				layout.getLocationOnScreen(location);
				builder.append("OnScreen : " + location[0] + " " + location[1]);
				
				mTextView.setText(builder.toString());
				
				for (int i = 1; i < layout.getChildCount() - 1; ++i) {
					setText((TextView) layout.getChildAt(i));
				}
			}
		});
		layout.addView(btn, params);
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		
	}
	
	private String getText(TextView textView) {
		return "width : " + textView.getWidth() + " height : " + textView.getHeight();
	}
	
	private void setText(TextView textView) {
		int[] location = new int[2];
		StringBuilder builder = new StringBuilder();
		textView.getLocationInWindow(location);
		builder.append("TextView LocInWindow : " + location[0] + " " + location[1]);
		textView.getLocationOnScreen(location);
		builder.append("OnScreen : " + location[0] + " " + location[1]);
		
		textView.setText(builder.toString());
	}
	
	
}
